<?php
//energy zsn 20171123 14:20
class EnergyModel extends Model{ 
	protected $serverName = "222.175.126.100"; 
	// protected $connectionInfo = array("UID"=>"jst", "PWD"=>"jst", "Database"=>"jfjc");
	protected $getTop3 = "SUM(CAST(s1 as float)+CAST(s2 as float)+CAST(s3 as float)) as a1,SUM(CAST(s4 as float)+CAST(s5 as float)+CAST(s6 as float)) as a2,SUM(CAST(s10 as float)+CAST(s11 as float)+CAST(s12 as float)) as a3,SUM(CAST(s13 as float)+CAST(s14 as float)+CAST(s15 as float)) as a4,SUM(CAST(s19 as float)+CAST(s20 as float)+CAST(s21 as float)) as a5,SUM(CAST(s22 as float)+CAST(s23 as float)+CAST(s24 as float)) as a6,SUM(CAST(s25 as float)+CAST(s26 as float)+CAST(s27 as float)) as a7,SUM(CAST(s28 as float)+CAST(s29 as float)+CAST(s30 as float)) as a8,SUM(CAST(s31 as float)+CAST(s32 as float)+CAST(s33 as float)) as a9,SUM(CAST(s34 as float)+CAST(s35 as float)+CAST(s36 as float)) as a10";
	function __destruct() {
		$conn = mssql_connect($this->serverName,'jst','jst');
        mssql_close($conn);
   }
	//默认显示3项最能耗
	//月能耗
	public function getMonthMaxZone(){
		$conn = mssql_connect($this->serverName,'jst','jst'); 
		if(($conn != false)&&(mssql_select_db('jfjc', $conn))) {
			if(date('d',time())!='1'){
				$sql = "select ".$this->getTop3." from fbhis where cid = '2' and txtime > '".date("Y-m-",time())."01 00:00:00' and txtime < '".date('Y-m-d',time())." 23:59:59';";
			}else{
				$sql = "select ".$this->getTop3." from fbhis where cid = '2' and convert(varchar(10),[txtime],120)='".date('Y-m-d',time())."'";
			}
			$query = mssql_query($sql);
			$top3 = $this->get_All_Sqlsrv_Common_Top3($query);
			$zone = array();
			foreach ($top3 as $k => $value) {
				switch ($k)
				{
					case 'a1':
					  $zone[] = '牌坊裡';
					  break;  
					case 'a2':
					  $zone[] = '味想家';
					  break;
					case 'a3':
					  $zone[] = '焙上の物语';
					  break;  
					case 'a4':
					  $zone[] = '真时';
					  break;
					case 'a5':
					  $zone[] = '体验中心';
					  break;  
					case 'a6':
					  $zone[] = '卫生间';
					  break;
					case 'a7':
					  $zone[] = '大厅';
					  break;  
					case 'a8':
					  $zone[] = '商超';
					  break;
					case 'a9':
					  $zone[] = '洗碗间';
					  break;  
					case 'a10':
					  $zone[] = '二楼用电';
					  break;
					default:
					  return false;
				}
			}
			return $zone;
		}else{
			return false;
		}
	}
	//点击区域能耗
	//月能耗
	public function getMonthConsume($zone){
		$conn = mssql_connect($this->serverName,'jst','jst'); 
		if(($conn != false)&&(mssql_select_db('jfjc', $conn))) {
			if(date('d',time())!='1'){
				$sql = "select ".$zone." from fbhis where cid = '2' and txtime > '".date("Y-m-",time())."01 00:00:00' and txtime < '".date('Y-m-d',time())." 23:59:59';";
				$sql2 = "select SUM(CAST(s1 as float)+CAST(s2 as float)+CAST(s3 as float)+CAST(s4 as float)+CAST(s5 as float)+CAST(s6 as float)+CAST(s10 as float)+CAST(s11 as float)+CAST(s12 as float)+CAST(s13 as float)+CAST(s14 as float)+CAST(s15 as float)+CAST(s19 as float)+CAST(s20 as float)+CAST(s21 as float)+CAST(s22 as float)+CAST(s23 as float)+CAST(s24 as float)+CAST(s25 as float)+CAST(s26 as float)+CAST(s27 as float)+CAST(s28 as float)+CAST(s29 as float)+CAST(s30 as float)+CAST(s31 as float)+CAST(s32 as float)+CAST(s33 as float)+CAST(s34 as float)+CAST(s35 as float)+CAST(s36 as float)) from fbhis where cid = '2' and txtime > '".date("Y-m-",time())."01 00:00:00' and txtime < '".date('Y-m-d',time())." 23:59:59';";
			}else{
				$sql = "select ".$zone." from fbhis where cid = '2' and convert(varchar(10),[txtime],120)='".date('Y-m-d',time())."'";
				$sql2 = "select SUM(CAST(s1 as float)+CAST(s2 as float)+CAST(s3 as float)+CAST(s4 as float)+CAST(s5 as float)+CAST(s6 as float)+CAST(s10 as float)+CAST(s11 as float)+CAST(s12 as float)+CAST(s13 as float)+CAST(s14 as float)+CAST(s15 as float)+CAST(s19 as float)+CAST(s20 as float)+CAST(s21 as float)+CAST(s22 as float)+CAST(s23 as float)+CAST(s24 as float)+CAST(s25 as float)+CAST(s26 as float)+CAST(s27 as float)+CAST(s28 as float)+CAST(s29 as float)+CAST(s30 as float)+CAST(s31 as float)+CAST(s32 as float)+CAST(s33 as float)+CAST(s34 as float)+CAST(s35 as float)+CAST(s36 as float)) from fbhis where cid = '2' and convert(varchar(10),[txtime],120)='".date('Y-m-d',time())."'";
			}
			$query = mssql_query($sql);
			$query2 = mssql_query($sql2);
			$result1 = mssql_fetch_array($query,MSSQL_NUM);
			$result2 = mssql_fetch_array($query2,MSSQL_NUM);
			$temp = $result1['0']/$result2['0'];
			$result['0'] = ceil($result1['0']);//本月耗能
			$result['1'] = ceil($temp*100);//本月耗能占比
			return $result;
		}else{
			return false;
		}
	}
	//周能耗
	public function getWeekConsume($zone){
		$conn = mssql_connect($this->serverName,'jst','jst'); 
		if(($conn != false)&&(mssql_select_db('jfjc', $conn))) {
			$sql = "select ".$zone." from fbhis where cid = '2' and (DATEPART(wk, txtime) = DATEPART(wk, GETDATE())) AND (DATEPART(yy, txtime) = DATEPART(yy, GETDATE()));";
			$sql2 = "select SUM(CAST(s1 as float)+CAST(s2 as float)+CAST(s3 as float)+CAST(s4 as float)+CAST(s5 as float)+CAST(s6 as float)+CAST(s10 as float)+CAST(s11 as float)+CAST(s12 as float)+CAST(s13 as float)+CAST(s14 as float)+CAST(s15 as float)+CAST(s19 as float)+CAST(s20 as float)+CAST(s21 as float)+CAST(s22 as float)+CAST(s23 as float)+CAST(s24 as float)+CAST(s25 as float)+CAST(s26 as float)+CAST(s27 as float)+CAST(s28 as float)+CAST(s29 as float)+CAST(s30 as float)+CAST(s31 as float)+CAST(s32 as float)+CAST(s33 as float)+CAST(s34 as float)+CAST(s35 as float)+CAST(s36 as float)) from fbhis where cid = '2' and  (DATEPART(wk, txtime) = DATEPART(wk, GETDATE())) AND (DATEPART(yy, txtime) = DATEPART(yy, GETDATE()));";
			$query = mssql_query($sql);
			$query2 = mssql_query($sql2);
			$result1 = mssql_fetch_array($query,MSSQL_NUM);
			$result2 = mssql_fetch_array($query2,MSSQL_NUM);
			$temp = $result1['0']/$result2['0'];
			$result['0'] = ceil($result1['0']);//本周耗能
			$result['1'] = ceil($temp*100);//本周耗能占比
			return $result;
		}else{
			return false;
		}
	}
	//日能耗
	public function getDayConsume($zone){
		$conn = mssql_connect($this->serverName,'jst','jst'); 
		if(($conn != false)&&(mssql_select_db('jfjc', $conn))) {
			$sql = "select ".$zone." from fbhis where cid = '2' and convert(varchar(10),[txtime],120)='".date('Y-m-d',time())."'";
			$sql2 = "select SUM(CAST(s1 as float)+CAST(s2 as float)+CAST(s3 as float)+CAST(s4 as float)+CAST(s5 as float)+CAST(s6 as float)+CAST(s10 as float)+CAST(s11 as float)+CAST(s12 as float)+CAST(s13 as float)+CAST(s14 as float)+CAST(s15 as float)+CAST(s19 as float)+CAST(s20 as float)+CAST(s21 as float)+CAST(s22 as float)+CAST(s23 as float)+CAST(s24 as float)+CAST(s25 as float)+CAST(s26 as float)+CAST(s27 as float)+CAST(s28 as float)+CAST(s29 as float)+CAST(s30 as float)+CAST(s31 as float)+CAST(s32 as float)+CAST(s33 as float)+CAST(s34 as float)+CAST(s35 as float)+CAST(s36 as float)) from fbhis where cid = '2' and convert(varchar(10),[txtime],120)='".date('Y-m-d',time())."'";
			$query = mssql_query($sql);
			$query2 = mssql_query($sql2);
			$result1 = mssql_fetch_array($query,MSSQL_NUM);
			$result2 = mssql_fetch_array($query2,MSSQL_NUM);
			$temp = $result1['0']/$result2['0'];
			$result['0'] = ceil($result1['0']);//本月耗能
			$result['1'] = ceil($temp*100);//本月耗能占比
			return $result;
		}else{
			return false;
		}
	}
	//室内外温湿度
	public function getInOutInfo(){
		$conn = mssql_connect($this->serverName,'jst','jst'); 
		if(($conn != false)&&(mssql_select_db('jfjc', $conn))) {
			$query = mssql_query("select outwd,outsd,inwd,insd from environment_ontime where cid = '2' order by txtime desc");
			$row = mssql_fetch_array($query,MSSQL_ASSOC);
			return $row;
		}else{
			return false;
		}
	}
	//机房滴水报警
	public function getAlertInfo(){
		$conn = mssql_connect($this->serverName,'jst','jst'); 
		if(($conn != false)&&(mssql_select_db('jfjc', $conn))) {
			$query = mssql_query("select ds,txtime from security_ontime where cid = '2' order by txtime desc");
			$row = mssql_fetch_array($query,MSSQL_ASSOC);
			return $row;
		}else{
			return false;
		}
	}
	//对应时段的电压平均值
	public function getVabc($date,$hours){
		$conn = mssql_connect($this->serverName,'jst','jst'); 
		if(($conn != false)&&(mssql_select_db('jfjc', $conn))) {
			$start = $date.' '.$hours;
			$onehours = intval($hours)+1;
			$end = $date.' '.str_pad($onehours,2,0,STR_PAD_LEFT);
			$sql = "select va,vb,vc from itmeter_his where cid = '2' and txtime > '".$start.":00:00' and txtime < '".$end.":00:00';";
			$query = mssql_query($sql);
			$row = $this->get_All_Sqlsrv_Vabc($query);
			return $row;
		}else{
			return false;
		}
	}
	//能耗列表
	public function getConsumptionList($inputs){
		$conn = mssql_connect($this->serverName,'jst','jst'); 
		if(($conn != false)&&(mssql_select_db('jfjc', $conn))) {
			$time_rages = explode(' - ', $inputs['searchTime']);
			$start = $time_rages['0'];//起始查询日期
			$end = $time_rages['1'];//结束查询日期
			if($start == $end){
				//查今天或自定义查一天
				$sql = "select txtime,s1,s2,s3,s4,s5,s6,s10,s11,s12,s13,s14,s15,s19,s20,s21,s22,s23,s24,s25,s26,s27,s28,s29,s30,s31,s32,s33,s34,s35,s36 from fbhis where cid = '2' and convert(varchar(10),[txtime],120)='".$end."' order by txtime desc";
			}else{
				//查多天
				$day = $inputs['page']?$inputs['page']:1;
				$day_before = intval($day)-1;
				if(!$day_before){
					//最新一天
 					$searchDay = $end;
				}else{
					//之前某天
					$str = '-'.$day_before.' day';
					$searchDay = date("Y-m-d",strtotime($str,strtotime($end)));
				}
				//当前不是第一页，判断page是否在新选择的日期之间
				$d1 = strtotime($start);
				$d2 = strtotime($end);
				$between = round(($d2-$d1)/86400)+1;
				if(intval($between)<intval($day)){
					$searchDay = $end;
				}
				$sql = "select txtime,s1,s2,s3,s4,s5,s6,s10,s11,s12,s13,s14,s15,s19,s20,s21,s22,s23,s24,s25,s26,s27,s28,s29,s30,s31,s32,s33,s34,s35,s36 from fbhis where cid = '2' and convert(varchar(10),[txtime],120)='".$searchDay."' order by txtime desc";
			}
			$query = mssql_query($sql);
			$result = $this->get_All_Sqlsrv($query);
			return $result;
		}else{
			return false;
		}
	}
	//获取结果集全部,能耗最高3项
	public function get_All_Sqlsrv_Common_Top3($query){
		$result = array();
		while($row = mssql_fetch_array($query,MSSQL_ASSOC)){
			$result[] = $row;
		}
		$all_list = $result[0];
		arsort($all_list);
		$arr = array_slice($all_list,0,3);
		return $arr;
	} 
	//获取结果集全部,并修改time格式为仅显示小时
	public function get_All_Sqlsrv($query){
		$result = array();
		while($row = mssql_fetch_array($query,MSSQL_ASSOC)){
			//取日期时仅按Y-m-d
        	$temp['date'] = date('Y-m-d',strtotime($row['txtime']));
        	$temp['time'] = date('H',strtotime($row['txtime']));
        	$row['date'] = $temp['date'];
        	$row['time'] = $temp['time'];
			$result[] = $row;
		}
		return $result;
	} 
	//获取结果集全部,并求电压平均值
	public function get_All_Sqlsrv_Vabc($query){
		$result = array();
		$count = 0;
		$temp['va'] = 0;
		$temp['vb'] = 0;
		$temp['vc'] = 0;
		while($row = mssql_fetch_array($query,MSSQL_ASSOC)){
			$count++;
			$temp['va'] += $row['va'];
			$temp['vb'] += $row['vb'];
			$temp['vc'] += $row['vc'];
		}
		$result['va'] = number_format($temp['va']/$count,1);
		$result['vb'] = number_format($temp['vb']/$count,1);
		$result['vc'] = number_format($temp['vc']/$count,1);
		return $result;
	} 
}